import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';
import * as argon2 from 'argon2';
@Injectable()
export class UserService {
  async create(createUserDto: CreateUserDto) {
    let password = await argon2.hash(createUserDto.password);

    return User.save({
      ...createUserDto,
      password,
      isShow: true,
      create_time: new Date(),
      update_time: new Date(),
      delete_time: null,
    });
  }

  async findAll(pageNum: number, pageSize: number) {
    const [list, count] = await User.findAndCount({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });
    return { list, count };
  }

  findOne(id: number) {
    return User.findOneBy({ id });
  }

  async update(id: number, updateUserDto: UpdateUserDto) {
    let password = await argon2.hash(updateUserDto.password);
    return User.update(id, { ...updateUserDto, password });
  }

  remove(id: number) {
    return User.update(id, { delete_time: new Date() });
  }
}
